﻿#pragma once
#include "strategy.h"
#include "receiver.h"
#include <param.hpp>


class ssg_1_strategy : public lt::strategy,public lt::signal_handler,public lt::trend_handler
{
	struct persist_data
	{
		uint32_t trading_day;

		estid_t order_estid;

		persist_data() :
			trading_day(0x0U),
			order_estid(INVALID_ESTID)
		{
		}
	};

public:
	
	ssg_1_strategy(lt::straid_t id, lt::engine& engine, bool openable, bool closeable, const lt::param& p):
		strategy(id, engine, openable, closeable),
		_signal(p.get<lt::sigid_t>("signal")),
		_trend(p.get<lt::trdid_t>("trend")),
		_open_once(p.get<uint32_t>("open_once")),
		_position_limit(p.get<uint32_t>("position_limit")),
		_cancel_second(p.get<uint32_t>("cancel_second")),
		_coming_to_close(0),
		_order_data(nullptr)
	{};


	~ssg_1_strategy(){};


public:


	/*
	 *	初始化事件
	 *	生命周期中只会回调一次
	 */
	virtual void on_init() override ;

	/*
	*	交易日初始化完成
	*/
	virtual void on_ready() override;

	/*
	*	趋势改变
	*/
	virtual void on_change(lt::trdid_t id, const code_t& code, lt::trend_action_type type) override;

	/*
	 *	信号处理器
	 */
	virtual void on_signal(lt::sigid_t sigid, const code_t& code, lt::signal_action_type type, double_t price) override;


	/*
	 *	订单接收回报
	 *  @is_success	是否成功
	 *	@order	本地订单
	 */
	virtual void on_entrust(const order_info& order) override;

	/*
	 *	成交回报
	 *
	 *	@localid	本地订单id
	 */
	virtual void on_trade(estid_t localid, const code_t& code, offset_type offset, direction_type direction, double_t price, uint32_t volume)  override;


	/*
	 *	撤单
	 *	@localid	本地订单id
	 */
	virtual void on_cancel(estid_t localid, const code_t& code, offset_type offset, direction_type directionv, double_t price, uint32_t cancel_volume, uint32_t total_volume)  override;

	/*
	 *	错误
	 *	@localid	本地订单id
	 *	@error 错误代码
	 */
	virtual void on_error(error_type type,estid_t localid, const uint32_t error) override;

	/*
	 *	销毁
	 */
	virtual void on_destory()override;

private:
	
	lt::sigid_t _signal;

	lt::trdid_t _trend ;
	
	uint32_t _open_once ;

	uint32_t _position_limit ;

	time_t _coming_to_close;

	persist_data* _order_data;

	uint32_t	_cancel_second;
};

